package com.test.reference;

import java.util.HashMap;
import java.util.WeakHashMap;

/**
 * todo
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/1/4
 */
public class WeakHashMapDemo {
    public static void main(String[] args) {

        myHashMap();
        System.out.println("========================");
        myWeakHashMap();
    }
    private static void myHashMap(){
        HashMap<Integer,String> weakHashMap = new HashMap<>();
        Integer key = new Integer(1);
        String value = "test";
        weakHashMap.put(key,value);
        System.out.println(weakHashMap);
        key = null;

        System.out.println(weakHashMap);
        System.gc();
        System.out.println(weakHashMap+"\t"+weakHashMap.size());
    }

    private static void myWeakHashMap(){
        WeakHashMap<Integer,String> weakHashMap = new WeakHashMap<>();
        Integer key = new Integer(1);
        String value = "test";
        weakHashMap.put(key,value);
        System.out.println(weakHashMap);
        key = null;

        System.out.println(weakHashMap);
        System.gc();
        System.out.println(weakHashMap+"\t"+weakHashMap.size());
    }
}
